package org.i1510.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.cxf.wsn.util.IdGenerator;
import org.i1510.common.bean.SessionUserBean;
import org.i1510.common.po.SysLog;
import org.i1510.common.service.ISysLogService;
import org.i1510.common.utils.DateUtils;
import org.i1510.common.utils.IDGenerator;
import org.i1510.common.utils.SpringContextUtil;
import org.i1510.common.utils.web.ResultMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * @author Administrator
 * 记录详细的请求操作日志
 */
public class SysLogInterceptor extends HandlerInterceptorAdapter {

	private static Logger logger = LoggerFactory
			.getLogger(SysLogInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		HandlerMethod handlerM = (HandlerMethod) handler;
		String className = handlerM.getMethod().getDeclaringClass().getName();
		String methodName = handlerM.getMethod().getName();

		ISysLogService iSysLogService = (ISysLogService) SpringContextUtil.getBean("iSysLogService");
		SysLog sysLog = new SysLog();
		sysLog.setFdId(IDGenerator.getGenerateID());
		sysLog.setFdCreateTime(DateUtils.getNow());
		sysLog.setFdInterCeptor("preHandle");
		sysLog.setFdCurrentTimeMillis(System.currentTimeMillis());
		String ip = request.getHeader("x-forwarded-for");  
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
			 ip = request.getHeader("Proxy-Client-IP");  			
		}else if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
			ip = request.getHeader("WL-Proxy-Client-IP");  
		}else if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
			ip = request.getRemoteAddr();  
		}
		sysLog.setFdAddr(ip);
		sysLog.setFdHost(request.getRemoteHost());
		
		HttpSession session = request.getSession();
		Object userSession = session.getAttribute("userSession");
		if (null != userSession) {
			SessionUserBean sessionUserBean = (SessionUserBean) userSession;
			sysLog.setFdCreator(sessionUserBean.getSysUser().getDocId());
		}
		sysLog.setFdClassName(className);
		sysLog.setFdMethodName(methodName);
		
		ResultMapper mapperRtn = ResultMapper.generateFailure();
		mapperRtn = iSysLogService.save(sysLog);
		return true;
	}
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

		HandlerMethod handlerM = (HandlerMethod) handler;
		String className = handlerM.getMethod().getDeclaringClass().getName();
		String methodName = handlerM.getMethod().getName();

		ISysLogService iSysLogService = (ISysLogService) SpringContextUtil.getBean("iSysLogService");
		SysLog sysLog = new SysLog();
		sysLog.setFdId(IDGenerator.getGenerateID());
		sysLog.setFdCreateTime(DateUtils.getNow());
		sysLog.setFdInterCeptor("afterCompletion");
		sysLog.setFdCurrentTimeMillis(System.currentTimeMillis());
		String ip = request.getHeader("x-forwarded-for");  
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
			 ip = request.getHeader("Proxy-Client-IP");  			
		}else if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
			ip = request.getHeader("WL-Proxy-Client-IP");  
		}else if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
			ip = request.getRemoteAddr();  
		}
		sysLog.setFdAddr(ip);
		sysLog.setFdHost(request.getRemoteHost());
	
		HttpSession session = request.getSession();
		Object userSession = session.getAttribute("userSession");
		if (null != userSession) {
			SessionUserBean sessionUserBean = (SessionUserBean) userSession;
			sysLog.setFdCreator(sessionUserBean.getSysUser().getDocId());
		}
		sysLog.setFdClassName(className);
		sysLog.setFdMethodName(methodName);
		
		if(null != ex){
			sysLog.setFdExMessage(ex.getMessage());
			StackTraceElement[] stackTraces = ex.getStackTrace();
			for(int i=0 ;i<stackTraces.length;i++){
				sysLog.setFdExStackTrace(stackTraces[i].toString()+sysLog.getFdExStackTrace());
			}
			
			sysLog.setFdExLocalizedMessage(ex.getLocalizedMessage());
			if(null != ex.getClass()){
				sysLog.setFdClassName(ex.getClass().getName());
			}
		}
		ResultMapper mapperRtn = ResultMapper.generateFailure();
		mapperRtn = iSysLogService.save(sysLog);

	}

}